<?php
/**
 * Created by PhpStorm.
 * User: 65736
 * Date: 2019/3/7 0007
 * Time: 20:59
 */

namespace app\common\widget;

/**
 * Class WidgetButton
 * @package app\common\widget
 * @method WidgetButton setButtonStylePrimary()
 * @method WidgetButton setButtonStyleDefault()
 * @method WidgetButton setButtonStyleSuccess()
 * @method WidgetButton setButtonStyleInfo()
 * @method WidgetButton setButtonStyleDanger()
 * @method WidgetButton setButtonTypeSubmit()
 * @method WidgetButton setButtonTypeReset()
 * @method WidgetButton setButtonTypeDefault()
 * @method WidgetButton setButtonSizeLg()
 * @method WidgetButton setButtonSizeSm()
 * @method WidgetButton setButtonSizeXs()
 */
class WidgetButton extends WidgetCommon
{

    /**
     * 按钮类型
     * @var string
     */
    var $buttonType = WidgetConst::btnTypeDefault;

    /**
     * 按钮样式
     * @var string
     */
    var $buttonStyle = WidgetConst::btnStyleDefault;

    /**
     * 按钮大小
     * @var string
     */
    var $buttonSize = WidgetConst::btnSizeSm;

    /**
     * 按钮文本
     * @var string
     */
    var $buttonText = '未定义';

    /**
     * 设置Action属性
     * @var array
     */
    private $action = [];

    /**
     * 是否是ajax请求
     * @var bool
     */
    //private $is_ajax = false;

    /**
     * 构造方法, 渲染挂件
     * @return mixed
     * @throws \think\Exception
     */
    function render()
    {
        $this->beforeRender();

        // 分配按钮文本
        self::$view->assign( 'text', $this->buttonText );
        // 渲染模版
        return $this->fetch();
    }

    /**
     * @return $this
     * @throws \think\Exception
     */
    private function beforeRender() {
        // 设置样式
        $this->setClass( [$this->buttonStyle, $this->buttonSize] );
        // 设置类型
        $this->setDomAttr('type', $this->buttonType );
        // 生成action属性
        $this->createAction();
        return $this;
    }


    /**
     * @throws \think\Exception
     */
    protected function initialize()
    {
        parent::initialize();
        // 设置默认class
        $this->setClass( 'btn' );
    }

    /**
     * __call 魔术方法
     * @param $name
     * @param $arguments
     * @return $this|WidgetButton
     */
    public function __call($name, $arguments)
    {
        // 匹配样式方法 setButtonStyle
        if ( strpos( $name, 'setButtonStyle' ) !==false ) {
            $style = str_replace( "setButtonStyle", "", $name );
            $const = "btnStyle".$style;
            $argument = WidgetConst::getConstant($const);
            return $this->setButtonStyle( $argument );
        }

        // 匹配按钮类型 setButtonType
        if ( strpos( $name, 'setButtonType' ) !== false ) {
            $type = str_replace("setButtonType", "", $name);
            $const = "btnType".$type;
            $argument = WidgetConst::getConstant($const);
            return $this->setButtonType( $argument );
        }

        // 匹配按钮大小
        if ( strpos( $name, 'setButtonSize' ) !== false ) {
            $size = str_replace('setButtonSize', "", $name);
            $const = "btnSize".$size;
            $argument = WidgetConst::getConstant($const);
            return $this->setButtonSize ( $argument );
        }


        return $this;
    }

    /**
     * 设置按钮样式
     * @param $style
     * @return $this
     */
    private function setButtonStyle( $style ) {
        $this->buttonStyle = $style;
        return $this;
    }

    /**
     * 设置按钮类型
     * @param $type
     * @return $this
     */
    private function setButtonType( $type ) {
        $this->buttonType = $type;
        return $this;
    }

    /**
     * 设置按钮文本
     * @param $text
     * @return $this
     */
    public function setButtonText( $text ) {
        $this->buttonText = $text;
        return $this;
    }

    /**
     * 设置按钮大小
     * @param $size
     * @return $this
     */
    private function setButtonSize ( $size ) {
        $this->buttonSize = $size;
        return $this;
    }

    /**
     * 设置Button操作地址
     * @param string $controller   控制器名称
     * @param string $action       操作方法名称
     * @param array $param         请求参数
     * @return $this
     */
    public function setAction( $controller, $action, array $param=array() ) {
        $this->action = compact('controller', 'action', 'param');
        return $this;
    }

    /**
     * 设置Button操作地址参数
     * @param array $param
     * @return $this
     */
    public function setActionParam( array $param ) {
        $this->action['param'] = isset( $this->action['param'] ) ? array_merge( $this->action['param'], $param ) : $param;
        return $this;
    }


    /**
     * 生成action属性
     * @return $this
     */
    private function createAction() {
        if ( $this->action ) {
            /**
             * @var string $controller
             * @var string $action
             * @var array $param
             */
            extract($this->action);
            $path = "{$controller}/{$action}";
            $this->setDomAttr('action', url( $path, $param ));
        }
        return $this;
    }

    /**
     * 获取Button操作地址参数
     * @return array
     */
    public function getAction(): array
    {
        return $this->action;
    }


    /**
     * 设置请求ajax
     * @param $jsCallBackFuncton
     * @return $this
     */
//    public function setAjax( $jsCallBackFuncton ) {
//        $this->is_ajax = true;
//        $this->setDomAttr('jsCallBack', $jsCallBackFuncton);
//        return $this;
//    }






}